# Table B.3 
rm(list=ls())

(WD <- getwd())
if (!is.null(WD)) setwd(WD)

indata0      = paste0(WD,"/_individual_data/_otherrawdata/","", collapse = NULL)

library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(dplyr)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)

data          = read_dta(paste(indata0,"LIV_US_CPI1.dta",sep=""))

data$fc_rev   = data$fc_corr
data$fc_err   = data$fc_err
data$cons     = data$consensus

plm_rev_1   = plm(fc_err ~ fc_rev, data=data,index=c("ID", "qdate"), model="within", subset = category ==1)
std_rev_1   = sqrt(diag(vcovDC(plm_rev_1, type = "HC1")))

plm_rev_2   = plm(fc_err ~ fc_rev, data=data,index=c("ID", "qdate"), model="within", subset = category ==2)
std_rev_2   = sqrt(diag(vcovDC(plm_rev_2, type = "HC1")))

plm_rev_3   = plm(fc_err ~ fc_rev, data=data,index=c("ID", "qdate"), model="within", subset = category ==3)
std_rev_3   = sqrt(diag(vcovDC(plm_rev_3, type = "HC1")))

plm_rev_4   = plm(fc_err ~ fc_rev, data=data,index=c("ID", "qdate"), model="within", subset = category ==4)
std_rev_4   = sqrt(diag(vcovDC(plm_rev_4, type = "HC1")))

plm_rev_5   = plm(fc_err ~ fc_rev, data=data,index=c("ID", "qdate"), model="within", subset = category ==5)
std_rev_5   = sqrt(diag(vcovDC(plm_rev_5, type = "HC1")))


plm_con_1   = plm(fc_err ~ cons, data=data,index=c("ID", "qdate"), model="within", subset = category ==1)
std_con_1   = sqrt(diag(vcovDC(plm_con_1, type = "HC1")))

plm_con_2   = plm(fc_err ~ cons, data=data,index=c("ID", "qdate"), model="within", subset = category ==2)
std_con_2   = sqrt(diag(vcovDC(plm_con_2, type = "HC1")))

plm_con_3   = plm(fc_err ~ cons, data=data,index=c("ID", "qdate"), model="within", subset = category ==3)
std_con_3   = sqrt(diag(vcovDC(plm_con_3, type = "HC1")))

plm_con_4   = plm(fc_err ~ cons, data=data,index=c("ID", "qdate"), model="within", subset = category ==4)
std_con_4   = sqrt(diag(vcovDC(plm_con_4, type = "HC1")))

plm_con_5   = plm(fc_err ~ cons, data=data,index=c("ID", "qdate"), model="within", subset = category ==5)
std_con_5   = sqrt(diag(vcovDC(plm_con_5, type = "HC1")))



stderror = list(std_rev_1, std_con_1, std_rev_2, std_con_2, std_rev_3, std_con_3, std_rev_4, std_con_4, std_rev_5, std_con_5)

stargazer(plm_rev_1, plm_con_1, plm_rev_2, plm_con_2, plm_rev_3, plm_con_3, plm_rev_4, plm_con_4, plm_rev_5, plm_con_5, 
          type = "text",
          se = stderror,
          out="_tables/table_b3.txt")
